package com.yellowpage.ndc.service;

import java.util.List;
import java.util.Map;

import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.ndc.data.Customer;
import com.yellowpage.ndc.data.Listing;
import com.yellowpage.ndc.dao.ListingDao;

public class ListingServiceImpl extends BaseServiceImpl implements
		ListingService {
	private ListingDao listingDao;

	public void setListingDao(ListingDao listingDao) {
		this.listingDao = listingDao;
	}

	/* Smooth generate Method */
	public Listing getListing(Long id) {
		return listingDao.get(id);
	}
	
	public Listing getListingByTel(String tel) {
		return listingDao.getListingByTel(tel);
	}

	public List<Listing> getAllListing() {
		return listingDao.getAll();
	}

	public List<Map> queryListingByPage(Map queryMap, PageRender pageRender) {

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"	select l.list_id,l.cust_id,(select c.name1 from db_ndc.CUSTOMER c where c.cust_id = l.cust_id) as cust_name,l.tel, "
								+ "          l.findway_id,l.source,date_format(l.modify_time,'%Y-%m-%d') as modify_time,l.modify_emp_id as modify_emp,l.note, "
								+ " 		   (select a.addr from db_ndc.ADDRESS a where a.addr_id = l.addr_id) as addr,l.sb,l.list_order"
								+ "  	  from db_ndc.LISTING l where 1 = 1 ")
				.addCondition(" and LIST_ID = :listId ")
				.addCondition(" and CUST_ID = :custId ")
				.addCondition(" and ADDR_ID = :addrId ")
				.addCondition(" and SUBNAME1 = :subname1 ")
				.addCondition(" and SUBNAME2 = :subname2 ")
				.addCondition(" and SUBNAME3 = :subname3 ")
				.addCondition(" and SUBNAME4 = :subname4 ")
				.addCondition(" and TEL = :tel ")
				.addCondition(" and TEL_EXT = :telExt ")
				.addCondition(" and FAX_FLAG = :faxFlag ")
				.addCondition(" and SB = :sb ")
				.addCondition(" and LIST_ORDER = :listOrder ")
				.addCondition(" and NOTE = :note ")
				.addCondition(" and LIST_STATUS = :listStatus ")
				.addCondition(
						" and date_format(MODIFY_TIME,'%Y-%m-%d') =  :modifyTime  ")
				.addCondition(" and MODIFY_EMP_ID = :modifyEmpId ")
				.addCondition(" and FINDWAY_ID = :findwayId ").addCondition(
						" and SOURCE = :source ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public void saveListing(Listing listing) {
		listingDao.save(listing);
	}

	public void updateListing(Listing listing) {
		listingDao.update(listing);
	}

	public void delete(List deleteList) {
		listingDao.remove(deleteList);
	}

}